<?php
class UninstallHandler
{
	private $m_database = NULL;
	private $m_tablePrefix = NULL;
	private $m_databaseName = NULL;

    public function __construct(DatabaseConnection $database)
    {
        $this->m_database = $database;
		$this->m_tablePrefix = $database->getTablePrefix();
		$this->m_databaseName = $database->getDatabaseName();
    }
	
	// Kollar om användaren är admin.
	public function isUserAdmin($username)
	{
		$sqlStatement = "SELECT admin FROM ".$this->m_tablePrefix."users WHERE username=?";
		
		if ($stmt = $this->m_database->PrepareStatement($sqlStatement))
		{
			$stmt->bind_param("s", $username);
			
			$stmt->execute();
			
			$stmt->bind_result($isAdmin);
			
			if ($stmt->fetch() == true)
			{
				if($isAdmin == "true")
				{
					return true;
				}
				return false;
			}
		}		
	}
	
	// Droppar tabellerna.
	public function dropNotesTable($tablePrefix)
	{
		$tablePrefix = $tablePrefix."notes";
		
		$query = "DROP TABLE ".$this->m_tablePrefix;
		
		if ($this->m_database->query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	public function dropUsersTable($tablePrefix)
	{
		$tablePrefix = $tablePrefix."users";
		
		$query = "DROP TABLE ".$this->m_tablePrefix;
		
		// Droppa båda tabellerna.
		
		
		if ($this->m_database->query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	// Droppar databasen.
	public function dropDatabase($databaseName)
	{
		$query = "DROP DATABASE ".$this->m_databaseName;
		
		if ($this->m_database->query($query) == true)
		{
			return true;
		}
		
		return false;	
	}
	
	// Ransa settings.php
	public function truncateSettings()
	{
		// Skickar in en tom-sträng till Settings.php
		$xhtml = "";
		
		// Skapa filen Settings.php
		$file = fopen("Settings.php", "w+");
		
		// Skriv till filen
		fwrite($file, $xhtml);
		
		// Stäng filen.
		fclose($file);
	}
	
	public function doLogout()
	{
	   $lw = new LoginView();
	   
	   // Anropar funktion för att ta bort cookies.
	   $lw->removeCookie();

	   // Tar bort sessionsvariablerna.
	   unset($_SESSION['uid']);
	   unset($_SESSION['username']);
	   unset($_SESSION['password']);
	}
}